/**
 * 会员中心 刷脸打卡
 */
$(document).ready(function(e) {	
	var $time= new Date();
	$("#searchBeginTime").val(getDays());
	$("#searchEndTime").val(getDays());

	search(1);
	initList();//初始化 用户
	initDeptList();//初始化 部门
	
	var companyName = localStorage.getItem('memberCompanyName');
	if(companyName&&companyName!=="null"){
		$("#myModalLabel").text(companyName);
	}
	//显示上班时间
	$('#addModal').on('show.bs.modal', function () {
		getjobtime();
	})
	
	
	
	$("#btn-save").click(function(){
		var memberToken = localStorage.getItem('memberToken');
		$startTime =$("#startwork").val();
		$endTime =$("#endwork").val();
		if(!$startTime){
			alert("请设置上班时间");return false;
		}
		if(!$endTime){
			alert("请设置下班时间");return false;
		}
		requestJson = {"startTime":$startTime,"endTime":$endTime};
		$.ajax({
	        url: '/euc/clientparams/setJobTime',
	        data:JSON.stringify(requestJson),
	        beforeSend: function(request) {
	            request.setRequestHeader("token",memberToken );
	            request.setRequestHeader("member",memberToken );
	        },
	        dataType: 'JSON',
	        async: true,
	        contentType:'application/json;charset=utf-8',
	        type: 'POST',
	        success: function (result, textStatus, response) {
	        	if(result.status === -2){
	        		alert("设置成功")
	        	}else{
	        		alert(result.message);
	        	}
	        	
		
	        },
	        error: function () {
	        	console.log("error");
	        }  
	    });
	})
	
})
function getjobtime(){
	var memberToken = localStorage.getItem('memberToken');
	$.ajax({
        url: '/euc/clientparams/getJobTime',
        data:'',
        beforeSend: function(request) {
            request.setRequestHeader("token",memberToken );
            request.setRequestHeader("member",memberToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'GET',
        success: function (result, textStatus, response) {

	
        },
        error: function () {
        	console.log("error");
        }  
    });
}


function getDays($m,$n){
	if(!$n){
		$n='-';
	}

	var $time= new Date();
	$year = parseInt($time.getFullYear());
	$month = parseInt($time.getMonth())+1;
	$day = parseInt($time.getDate());
	if(!$m){
		if($month<10){
			$month ='0'+ $month;
		}
		if($day<10){
			$day ='0' + $day;
		}
		return $year +"-"+ $month +"-"+$day;	
	}
	if($n=='-'){
		$month =$month-$m;	
	}else{
		$month =$month+$m;	
	}

	if($month<=0){
		$year =$year -1;
		$month = $month +12;
	}else if($month>12){
		$year =$year +1;
		$month =1;
	}
	return $year +"-"+ $month +"-"+$day;	
}
/*初始化选项*/
function searchReset(){
	$("#searchBeginTime").val('');
	$("#searchEndTime").val('');
	$("#searchSpace").val(0);
	$("#searchStatus").val(0);
	$("#searchUserName").val('');
	$("#searchApplyUserName").val('');
	$("#searchMeetingRoomName").val('');
}

function getSearchParameter(currentPage){
	try{
		var btime = $("#searchBeginTime").val(),
			etime = $("#searchEndTime").val(),
			//deptid = parseInt($("#searchDept").val()),
			userid = $("#userStatus").val();
			
		if(!currentPage){
			currentPage=1;
		}
		//if(userid==0) userid="";

		//requestJson ={"data":{"end":etime,"start":btime,'name':userid},"currentPage":currentPage};
		requestJson ={"end":etime,"start":btime,'userIds':userid,'index':currentPage,'size':20};
		return requestJson;
	}catch(e){
		console.log(e.messge);
	}
	return false;	
}

function search(currentPage){
	var memberToken = localStorage.getItem('memberToken');
	var requestJson = getSearchParameter(currentPage);
	if(requestJson == false){
		return false;
	}
	$.ajax({
        url: '/euc/_onlineops/workontime/query',
        data:requestJson,
        beforeSend: function(request) {
            request.setRequestHeader("token",memberToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/x-www-form-urlencoded',
        type: 'GET',
        success: function (result, textStatus, response) {
        	if(result.status !== 0){
        		alert(result.message);return false;
        	}

        	//alert(JSON.stringify(result));
        	pageBean = result.data;
        	$("#userTable").empty();	
        	
        	$.each(result.data.list,function(i,data){
        		       		

        		var bgcolor= localStorage.getItem('trcolor');
        		if(i % 2 == 0){
        			bgcolor = '';
        		}
        		var html = '<tr '+ bgcolor +'>';
        		html += '<td>'+data.userName+'</td>';
        		html += '<td>'+data.deptName+'</td>';
        		html += '<td>'+data.date+'</td>';
        		html += '<td>'+data.checkInTime+'</td>';
        		html += '<td>'+(data.status1==1 ? '正常':'')+(data.status1==0 ? '缺卡':'')+'</td>';
        		html += '<td>'+data.checkOutTime+'</td>';
        		html += '<td>'+(data.status2==1 ? '正常':'')+(data.status2==0 ? '缺卡':'')+'</td>';
        	//	html += '<td>'+(data.reason1 ? '上午'+data.reason1 :'')+(data.reason2 ? '下午'+data.reason2 :'')+'</td>';
  
        		html += '</tr>';
	        	$("#userTable").append(html);
	        	//dataList[data.id] = data;
			});
        	$("#totalRow").html(pageBean.totalRow+'&nbsp;');
        	$("#currentPage").html(pageBean.currentPage+'&nbsp;');
        	$("#pageSize").html(pageBean.pageSize+'&nbsp;');        	
        	var pageLength = 15;
        	initailPageNumber(pageBean.totalPage,pageBean.currentPage,pageLength);        
        },
        error: function () {
        	console.log("error");
        }  
    });
}

//获取部门
function initDeptList(){
	deptId = localStorage.getItem('memberDeptId');
	companyName = localStorage.getItem('memberCompanyName');
	isAdmin = parseInt(localStorage.getItem('memberAdmin'));
	if(!companyName) companyName='部门列表';
	$(".left_title").html(companyName);
	if(isAdmin==1){
		$(".left_button").html('<button type="button" data-target="#addModal" class="btn btn-default" data-backdrop="static" data-toggle="modal" style="background: #fff;color: #000;border:1px solid;">新增部门</button>');
	}

	$(".left_radios").html('<div class="radio"><label><input type="radio" name="optionsRadios" id="optionsRadios1" value="0">全部</label></div>');
	

	$.ajax({
        url: '/euc/center/member/list',
        data:'',
        beforeSend: function(request) {
        	var memberToken = localStorage.getItem('memberToken');
            request.setRequestHeader("token",memberToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/x-www-form-urlencoded',
        type: 'POST',
        success: function (result, textStatus, response) {
        	//alert(JSON.stringify(result));
        	$.each(result,function(i,v){

        		html = '<option value="'+ v.id+'">'+ v.deptName+'</option>';
	        	$("#searchDept").append(html);
        	})
        },
        error: function () {
        	console.log("error");
        }  
    });
}
//获用户
function initList(){
	deptId = localStorage.getItem('memberDeptId');
	isAdmin = parseInt(localStorage.getItem('memberAdmin'));

	if(isAdmin==1){
		$("#rightbtn").html('<button type="button" class="btn btn-default" data-target="#addMemberModal" data-backdrop="static" data-toggle="modal" style="background: #fff;color: #000;border:1px solid;margin-right:30px;">新增员工</button><button type="button" class="btn btn-default"data-target="#MemberModal" data-backdrop="static" data-toggle="modal" style="background: #fff;color: #000;border:1px solid;">导入员工</button>');
	}
	$.ajax({
		url: '/euc/_user/member_list',
		data:{"dept_id":deptId},
		beforeSend: function(request) {
			var memberToken = localStorage.getItem('memberToken');
			request.setRequestHeader("token",memberToken );
		},
		dataType: 'JSON',
		async: true,
		contentType:'application/x-www-form-urlencoded',
		type: 'GET',
		success: function (result, textStatus, response) {
			//alert(JSON.stringify(result));
			if(result.status === -2){
        		gotoLogin();
        	}
        	//alert(JSON.stringify(result));
        	pageBean = result.data;
        	$("#userTable").empty();	
        	
        	$.each(result.data,function(i,data){
        		       		
        		html = '<option value="'+ data.member_id+'">'+ data.member_name+'</option>';
	        	$("#userStatus").append(html);
			});

		},
		error: function () {
			console.log("error");
		}  
	});
}

function clearForm(){
    $("input[name='user_name']").val('');
    $("input[name='userName']").val('');
    $("input[name='name']").val('');
    $("select[name='userGender']").val('男').trigger('change');
    $("input[name='age']").val('');
    $("input[name='email']").val('');
    $("input[name='idCard']").val('');
    $("#mobile").val('');
    $("input[name='userPassword']").val('');
    $("input[name='rePassword']").val('');

    $("input[name='idCard']").val('');

    $("#space_id").val('0').trigger('change');
    $("#client_id").val('0').trigger('change');

}
function formatDateTime(inputTime) {    
	var l = ["日","一","二","三","四","五","六"];
    var date = new Date(inputTime);  
    var y = date.getFullYear();    
    var m = date.getMonth() + 1;    
    m = m < 10 ? ('0' + m) : m;    
    var d = date.getDate();    
    d = d < 10 ? ('0' + d) : d;    
    var day = date.getDay();
    var str = "周" + l[day];
    
    return y + '/' + m + '/' + d+' '+str;    
};  

function formatTime(inputTime) {    
    var date = new Date(inputTime);  
    
    var h = date.getHours();  
    h = h < 10 ? ('0' + h) : h;  
    var minute = date.getMinutes();  
    minute = minute < 10 ? ('0' + minute) : minute;    
    return h+':'+minute;    
};  